package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;

import com.example.myapplication.adapter.MyRecyclerviewAdapter;
import com.example.myapplication.databinding.ActivityMainBinding;
import com.example.myapplication.entity.RecipeModel;
import com.example.myapplication.entity.ResultsBean;
import com.example.myapplication.service.MyAppService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    Retrofit retrofit=new Retrofit.Builder()
            .baseUrl("https://liyuankun.cn/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    MyAppService myAppService=retrofit.create(MyAppService.class);
    ActivityMainBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this);
        binding.rvRecipe.setLayoutManager(linearLayoutManager);
        getAllRecipte();
    }

    private void getAllRecipte() {
        Call<RecipeModel> recipe= myAppService.getRecipe(1000);
        recipe.enqueue(new Callback<RecipeModel>() {
            @Override
            public void onResponse(Call<RecipeModel> call, Response<RecipeModel> response) {
                List<ResultsBean> list;
                RecipeModel re= response.body();
                list= re.getResults();

                MyRecyclerviewAdapter myRecyclerviewAdapter = new MyRecyclerviewAdapter(list, MainActivity.this);
                binding.rvRecipe.setAdapter(myRecyclerviewAdapter);
            }

            @Override
            public void onFailure(Call<RecipeModel> call, Throwable t) {

            }
        });
    }
}
